<style type="text/css">
h3 { margin: 0px; margin-top: 12px; margin-bottom: 6px; }
code { color: #333366; font-style: italic; }
code.opt { font-weight: bold; font-style: normal; }
.error { color: #993333; }
.warning { color: #cc33cc; }
</style>
<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('admin.php');

$title = __('Cache','lp');
$this_file = 'lightpress/wp-plugin/cache.php';
$parent_file = 'lightpress/wp-plugin/main.php';

$submenu[$this_file][] = array(
    __('Cache','lp'),
    6,
    'lightpress/wp-plugin/cache.php',
    __('LightPress Cache','lp')
    );

require_once 'LightPressOptions.php';

$lp_opts = new LightPressOptions();

?>
<div class="wrap">
<h2><?php _e('LightPress Cache','lp'); ?></h2>
<p>
<?php _e('Enable the cache <strong>only if you really need it</strong>, as it may make things worse for your blog and your service provider. Similarly to <a href="http://mnm.uib.es/gallir/wp-cache-2/">other cache engines</a>, this cache works by storing a copy of each page in the filesystem, with an additional copy for each identified user (eg having a WordPress or LightPress cookie set). The stored pages expire after a certain amount of time (<code>validity</code> set below), but <strong>they are phisically removed from the filesystem after expiration only when the garbage collector runs</strong> (<code>gc_probability</code> set below).','lp'); ?> 
</p>
<p>
<?php _e('This means that if you have a high traffic blog, lots of posts, and many identified users (people who at some time have posted a comment on your blog), you will end up with lots of cache files, most of which will be only marginally useful. Your blog will be slower for most users, <strong>the filesystem will be full of unneeded junk</strong>, and your service provider will most likely become mad at you.', 'lp'); ?>
</p>
<p>
<?php _e('A few cases where caching might be useful are when', 'lp'); ?>
<ul>
<li><?php _e('one of your posts is or might get slashdotted (many anonymous visitors requesting a few pages in a short time)', 'lp'); ?></li>
<li><?php _e('lots of anonymous users frequently request your index page to check for updates', 'lp'); ?></li>
</ul>
<?php _e('If you have one of the above situations, try to enable caching (we have made it a manual process so you will not enable it by mistake), <strong>set the appropriate inclusions/exclusions</strong> (eg in the examples above only include the posts which might get slashdotted, and/or the index page), and monitor your server load and your filesystem usage to see if it really helps. <strong>Caching is not yet enabled for feeds</strong> as they already use Conditional GET. We will add feeds support in one of the next releases.', 'lp'); ?>
</p>

<?php
$ret = include_once($lp_opts->basedir . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'FileCache.php');

if (is_null($lp_opts->basedir)) {
    echo '<h3>' . __('LightPress configuration not found', 'lp') . '</h3><p>' . __('please <a href="admin.php?page=lightpress/wp-plugin/main.php">check the folder where you have install LightPress</a>, then set (and save) required options.</p>', 'lp');
} else if (!$ret || !class_exists('LightPressCache')) {
    // if we ever have more than one cache classes, we will need to change this
    echo class_exists('LightPressCache') . '<h3>' . __('Cache class not found', 'lp') . '</h3>';
} else {
    
    // ugly code following, replace with something more modular
    
    $lp_cache_args = get_option('lp_opt_cache_args');

    $lp_base_url = parse_url(get_option('lp_opt_url'));
    if (isset($lp_base_url['path']))
        $lp_cache_base_path = $lp_base_url['path'] . '/';
    else
        $lp_cache_base_path = '/';
    
    if (!is_array($lp_cache_args) || !is_object($lp_cache)) {
        // might already be instantiated by the LP clean-up actions
        if (!is_array($lp_cache_args)) {
            $lp_cache_args = array();
            $lp_cache_args['base_path'] = $lp_cache_base_path;
        } else {
            $lp_cache_args['dir'] = stripslashes($lp_cache_args['dir']);
        }
        $lp_cache =& new LightPressCache($lp_cache_args, true);
    }
    
    $lp_opt_cache_actions_enabled = get_option('lp_opt_cache_actions_enabled');
    $lp_opt_cache_actions_all_pages = get_option('lp_opt_cache_actions_all_pages');
    
    
    foreach ($lp_cache->constructor_args as $k)
        $lp_cache_args[$k] = $lp_cache->$k;
    
    if (isset($_POST['lp_action'])) {
        
        if (isset($_POST['lp_opt_cache_actions_enabled']))
            $lp_opt_cache_actions_enabled = $_POST['lp_opt_cache_actions_enabled'];
        else
            $lp_opt_cache_actions_enabled = 0;
        $lp_opts->updateOption('lp_opt_cache_actions_enabled', $lp_opt_cache_actions_enabled);

        if (isset($_POST['lp_opt_cache_actions_all_pages']))
            $lp_opt_cache_actions_all_pages = $_POST['lp_opt_cache_actions_all_pages'];
        else
            $lp_opt_cache_actions_all_pages = 0;
        $lp_opts->updateOption('lp_opt_cache_actions_all_pages', $lp_opt_cache_actions_all_pages);
        
        if (!isset($_POST['lp_cache_arg_track_users']))
            $_POST['lp_cache_arg_track_users'] = '0';
        
        $lp_cache_args_modified = false;
        
        foreach ($_POST as $k=>$v) {
            if (strpos($k, 'lp_cache_arg_') === 0) {
                $lp_cache_args_modified = true;
                $lp_cache_args[substr($k, 13)] = $v;
            }
        }
        
        if ($lp_cache_args_modified) {
            $lp_cache_args['base_path'] = $lp_cache_base_path;
            $lp_opts->updateOption("lp_opt_cache_args", $lp_cache_args);
        }
        
        $lp_cache_args = get_option('lp_opt_cache_args');
    }
    
    echo '<form name="form1" method="post" action="admin.php?page=lightpress/wp-plugin/cache.php">
    
    <fieldset class="options">
    <legend>'.__('Cache Options', 'lp').'</legend>
    <table width="800" cellspacing="2" cellpadding="5" class="editform">
    
    <tr><th width="180" valign="top" scope="row">'.__('validity', 'lp').'</th>
    <td><input type="text" name="lp_cache_arg_validity" value="' . $lp_cache_args['validity'] . '" /><br />
    <em>'.__('How long does a cache stay valid, in seconds', 'lp').'</em><br /></td></tr>
    <tr><th width="180" valign="top" scope="row">'.__('gc probability', 'lp').'</th>
    <td><input type="text" name="lp_cache_arg_gc_probability" value="' . $lp_cache_args['gc_probability'] . '" /><br />
    <em>'.__('Remove expired cache files once every n non-cached requests.', 'lp').'</em></td></tr>
    <tr><th width="180" valign="top" scope="row">'.__('storage folder', 'lp').'</th>
    <td><input type="text" size="65" name="lp_cache_arg_dir" value="' . $lp_cache_args['dir'] . '" /><br />
    <em>'.__('Where to store cache files.', 'lp').'</em></td></tr>

    <tr><th width="180" valign="top" scope="row">'.__('Track identified users', 'lp').'</th>
    <td>
    <input type="checkbox" name="lp_cache_arg_track_users" value="1" ' .
        ($lp_cache_args['track_users'] == '1' ? 'checked="on" ' : '') . ' /><br />
    <em>'.__('check this to save a personalized copy for identified users, uncheck to always serve them dynamic content','lp').'</em></td></tr>
    
    <tr><th width="180" valign="top" scope="row">'.__('URL check method','lp').'</th>
    <td>
    <input type="radio" name="lp_cache_arg_include_or_exclude" value="exclude"' .
        ($lp_cache_args['include_or_exclude'] == 'exclude' ? 'checked="on" ' : '') . ' /> '.__('exclude','lp').' 
    <input type="radio" name="lp_cache_arg_include_or_exclude" value="include"' .
        ($lp_cache_args['include_or_exclude'] == 'include' ? 'checked="on" ' : '') . ' /> include<br />
    <em>'.__('controls the scope of the URL list below: \'exclude\' caches all URLs except those listed below, \'include\' caches only the URLs listed below','lp').'</em></td></tr>
    
    <tr><th width="180" valign="top" scope="row">'.__('URL list','lp').'</th>
    <td><input type="text" size="65" name="lp_cache_arg_url_list" value="' . $lp_cache_args['url_list'] . '" /><br />
    <em>'.__('URL paths without a trailing slash to include/exclude separated by spaces, eg /mypath/myblog/post/some-post','lp').'</em></td></tr>
    
    </table>
    </fieldset>

    <fieldset class="options">
    <legend>'.__('WP Integration','lp').'</legend>
    <table width="800" cellspacing="2" cellpadding="5" class="editform">

    <tr><th width="180" valign="top" scope="row">'.__('enable WP filters','lp').'</th>
    <td>
    <input type="checkbox" name="lp_opt_cache_actions_enabled" value="1" ' .
        ($lp_opt_cache_actions_enabled == '1' ? 'checked="on" ' : '') . ' /><br />
    <em>'.__('enable the WP filters that clear the cache on new/modified post, comment, etc.','lp').'</em></td></tr>
    
    <tr><th width="180" valign="top" scope="row">'.__('clear all pages','lp').'</th>
    <td>
    <input type="checkbox" name="lp_opt_cache_actions_all_pages" value="1" ' .
        ($lp_opt_cache_actions_all_pages == '1' ? 'checked="on" ' : '') . ' /><br />
    <em>'.__('check to let WP filters wipe out all cached pages on a post/comment modification, default is to only remove the post page','lp').'</em></td></tr>
    </table>
    </fieldset>

    <p class="submit">
    <input type="submit" name="lp_action" value="'.__('Save','lp').' &raquo;" />
    </p>
    </form>

    <fieldset class="options">
    <legend>'.__('Cache Activation','lp').'</legend>
    <ol>
    <li><strong>'.__('have the cache storage folder writable by the web server process','lp').'</strong><br />
    <code>' . $lp_cache_args['dir'] . '</code>';
    
    if (is_dir($lp_cache_args['dir'])) {
        _e('exists','lp');
        if (is_writable($lp_cache_args['dir']))
            echo ' '.__('and is writable, ok','lp');
        else
            echo ' '.__('but is not writable by the server','lp');
    } else {
        _e('needs to be created','lp');
    }
    _e('<li><strong>activate the cache bootstrapping file</strong><br />','lp');
    $lp_cache_dir = $lp_opts->basedir . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
    printf(__('create the <code>%s\init.php</code> file. <strong>You should update the file whenever you change the <code>url</code> option.</strong> Copy and paste the following to the file','lp'), $lp_cache_dir); 
    echo '<pre class="php" style="padding: 3px; border: solid 1px; font-size: 11px;"><code style="font-style: normal;">';
    $_url_tokens = parse_url($lp_opts->getOption('url'));
    
    echo htmlentities('<?php

// '.__('include the Cache class definition','lp').'
$ret = @include_once(\'FileCache.php\');

// '.__('export the LP cache settings','lp').'
$lp_cache_settings = array(
    \'validity\'           => ' . $lp_cache_args['validity'] . ', // '.__('seconds','lp').'
    \'gc_probability\'     => ' . $lp_cache_args['gc_probability'] . ', // '.__('one in n+1 chances of doing garbage collection','lp').'
    \'dir\'                => \'' . $lp_cache_args['dir'] . '\',
    \'track_users\'        => ' . ($lp_cache_args['track_users'] == '1' ? 'true' : 'false') . ',
    \'include_or_exclude\' => \'' . $lp_cache_args['include_or_exclude'] . '\',
    \'url_list\'           => \'' . $lp_cache_args['url_list'] . '\',
    \'base_path\'          => \'' . $lp_cache_base_path . '\');

if ($ret && class_exists(\'LightPressCache\')) {
    // '.__('retrieve the cache settings','lp').'
    $cache =& new LightPressCache($lp_cache_settings);
    $cache->activate();
    if ($cache->serve_pages) {
        $cached_page = $cache->getCache();
        if (!empty($cached_page)) {
            header(\'Content-Size: \' . strlen($cached_page));
            echo $cached_page;
            exit();
        }
    }
}

?>');
    echo '</pre>
    </li>
    <li><strong>uncomment the following line in <code>' . $lp_opts->basedir . DIRECTORY_SEPARATOR . 'config.php</code></strong><br />
    <pre style="padding: 3px; border: solid 1px; font-size: 11px;" class="php">// include_once \'cache/init.php\';</pre>
    </li>
    </ol>
   </fieldset>';
    
}

?>
</div>
